﻿using DevExpress.Utils;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms;

namespace Extra.Data.Extra.Center
{

    public class BaseButton : PictureBox
    {
        internal DevExpress.Utils.ImageCollection images = null;

        public event EventHandler ButtonClick;

        public BaseButton()
        {
            this.BackColor = Color.Transparent;
            base.SizeMode = PictureBoxSizeMode.AutoSize;
            this.CreateImages();
            this.Image = this.images.Images[0];
        }

        protected virtual void CreateImages()
        {
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Left)
            {
                this.Image = this.images.Images[2];
            }
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            this.Image = this.images.Images[1];
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            this.Image = this.images.Images[0];
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            if (e.Button == MouseButtons.Left)
            {
                Rectangle rectangle = new Rectangle(0, 0, base.Width, base.Height);
                if (rectangle.Contains(new Point(e.X, e.Y)))
                {
                    this.Image = this.images.Images[1];
                    this.RaiseButtonClick();
                }
                else
                {
                    this.Image = this.images.Images[0];
                }
            }
        }

        private void RaiseButtonClick()
        {
            if (this.ButtonClick != null)
            {
                this.ButtonClick(this, EventArgs.Empty);
            }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public System.Drawing.Image Image
        {
            get
            {
                return base.Image;
            }
            set
            {
                base.Image = value;
            }
        }
    }
}

